package com.stable.actor.remotedemo;

import com.stable.actor.config.ActorSystemConfig;
import com.stable.actor.core.ActorRef;
import com.stable.actor.core.ActorSystem;
import com.stable.actor.core.Props;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class SimpleLocalDemoClient {

    public static void main(String[] args) throws InterruptedException {
        log.info("🚀 启动客户端...");

        // 客户端使用不同的端口（例如 2552），避免与服务器端口冲突
        ActorSystemConfig clientConfig = new ActorSystemConfig();


        // 服务器配置
        int serverPort = 2551; // 服务器端口
        String host = "localhost";

        if (args.length > 0) {
            host = args[0];
        }
        if (args.length > 1) {
            serverPort = Integer.parseInt(args[1]);
        }
        if(args.length > 2){
            clientConfig.setSystemPort(Integer.parseInt(args[2]));
        }

        // 创建客户端 ActorSystem
        ActorSystem system = new ActorSystem("ClientSystem", clientConfig);

        log.info("🔗 连接服务器: {}:{}", host, serverPort);
        log.info("🖥️ 客户端系统端口: {}", system.getSystemPort());

        try {
            // 第二步：创建远程引用 - 指向服务器的单一端口
            log.info("🔗 创建远程服务引用...");

            ActorRef paymentRemoteRef = system.remoteActorOf(host, serverPort, "/user/payment-service");
            ActorRef inventoryRemoteRef = system.remoteActorOf(host, serverPort, "/user/inventory-service");
            ActorRef notificationRemoteRef = system.remoteActorOf(host, serverPort, "/user/notification-service");
            ActorRef orderRemoteRef = system.remoteActorOf(host, serverPort, "/user/order-service");

            log.info("✅ 远程引用创建完成:");
            log.info("  - Payment: {}", paymentRemoteRef.path());
            log.info("  - Inventory: {}", inventoryRemoteRef.path());
            log.info("  - Notification: {}", notificationRemoteRef.path());
            log.info("  - Order: {}", orderRemoteRef.path());

            // 检查远程服务连通性
            log.info("🔍 检查远程服务连通性...");
            checkServiceAvailability(paymentRemoteRef, "Payment");
            checkServiceAvailability(inventoryRemoteRef, "Inventory");
            checkServiceAvailability(notificationRemoteRef, "Notification");
            checkServiceAvailability(orderRemoteRef, "Order");

            // 第三步：发送测试订单 - 直接发送到远程订单服务
            log.info("\n📤 发送测试订单到远程服务...");
            Order testOrder = new Order("TEST-001", "user-123", "iPhone 15", 1, 5999.0);

            // 方式1：直接发送到远程订单服务
            orderRemoteRef.tell(new CreateOrderCommand(testOrder), ActorRef.noSender());

            // 等待处理完成
            Thread.sleep(10000);

        } finally {
            log.info("\n🛑 关闭客户端系统...");
            system.shutdown();
        }

        log.info("🎉 客户端测试完成!");
    }

    /**
     * 检查服务可用性
     */
    private static void checkServiceAvailability(ActorRef serviceRef, String serviceName) {
        if (serviceRef instanceof com.stable.actor.core.RemoteActorRef) {
            com.stable.actor.core.RemoteActorRef remoteRef = (com.stable.actor.core.RemoteActorRef) serviceRef;
            boolean reachable = remoteRef.isReachable();
            log.info("  - {}服务: {} {}", serviceName, reachable ? "✅ 可达" : "❌ 不可达", remoteRef.getNetworkAddress());
        } else {
            log.info("  - {}服务: ⚠️ 本地引用", serviceName);
        }
    }
}
